package com.springboot.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.springboot.feign.VirtualWalletFeign;
import com.springboot.model.po.BankCard;
import com.springboot.model.po.VirtualWalletPO;
import com.springboot.model.vo.VirtualWalletVO;
import com.springboot.service.BankCardService;

import util.VerifyUtil;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/bankCards")
public class BankCardController {
	@Autowired
	private BankCardService bankCardService;
	@Autowired
	private VirtualWalletFeign virtualWalletFeign;
	//基于feign不需要restTemplate和DiscoveryClient
//	@Autowired
//	private DiscoveryClient dicoveryClient;
//	@Autowired
//	private RestTemplate restTemplate;
//	@Bean
//	public RestTemplate restTemplate() {
//		return new RestTemplate();
//	}
	
	@PostMapping
	public ResponseEntity saveBankCard(@RequestBody BankCard bankCard , @RequestParam String token) throws Exception{
		//#{userName},#{bankName},#{walletId}
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null)
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("WITH WRONG TOKEN!");

		ResponseEntity<VirtualWalletVO> result_wl = virtualWalletFeign.getVirtualWalletByUserId(userId_target, token);
		if(result_wl.getStatusCode().equals(HttpStatus.FORBIDDEN))
			return ResponseEntity.status(HttpStatus.FORBIDDEN).body("FORBBIDEN!");
		if(!result_wl.getBody().getWalletId().equals(bankCard.getWalletId())){
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("WITH WRONG ARGS!");
		}
		int result = bankCardService.saveBankCard(bankCard);
		if(result == 0) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred");
		}else {
			return ResponseEntity.ok("ok");
		}
	}
	
	@GetMapping("/{walletId}")
	public ResponseEntity listBankCardByWalletId(@PathVariable Integer walletId , @RequestParam String token) throws Exception{
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null)
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("WITH WRONG TOKEN!");
		
//		List<ServiceInstance> instance_wl = dicoveryClient.getInstances("virtualwallet-server");
//		ServiceInstance instance = instance_wl.get(0);
//		String url = "http://"+instance.getHost()+":"+instance.getPort()+"/virtualWallets/users"+userId_target+"?"+"token="+token;
//		HashMap result_wl = restTemplate.getForObject(url, HashMap.class);
		
		//返回的类型和接收的必须一致
		ResponseEntity<VirtualWalletVO> result_wl = virtualWalletFeign.getVirtualWalletByUserId(userId_target, token);
		//熔断响应
		if(result_wl.getStatusCode().equals(HttpStatus.FORBIDDEN)) {
			return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ArrayList());
		}
		//信息不一致
		if(!result_wl.getBody().getWalletId().equals(walletId)){
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("WITH WRONG ARGS!");
		}
		List<BankCard> list = bankCardService.listBankCardByWalletId(walletId);
		//没有walletId对应的bankcard
		if(list.size() == 0 || list == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Wrong walletId!");
		}else {
			return ResponseEntity.ok(list);
		}
	}

}
